<script lang="ts" setup>
import { reactive } from 'vue';
import { loginUsingPost } from "@/api/userController";
import { useLoginUserStore } from "@/stores/UseLoginUserStore";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';

const loginUserStore = useLoginUserStore()
const router = useRouter()

interface FormState {
  userAccount: string;
  userPassword: string;
}

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
});

const handleSubmit = async (values: any) => {
  try {
    const res = await loginUsingPost(values)
    if (res.data.code === 0 && res.data.data) {
      await loginUserStore.getLoginUser()
      message.success('登录成功')
      router.push({
        path: '/',
        replace: true
      })
    } else {
      message.error('登录失败')
    }
  } catch (e: any) {
    message.error('登录失败' + e.message)
  }
};
</script>

<template>
  <div class="login-container">
    <div class="animated-background">
      <div class="shape circle"></div>
      <div class="shape triangle"></div>
      <div class="shape square"></div>
    </div>

    <div class="login-card">
      <div class="header">
        <h1 class="title">企业级智能协同云图库</h1>
        <p class="subtitle">探索无限可能 智绘未来蓝图</p>
      </div>

      <a-form
          :model="formState"
          class="login-form"
          @finish="handleSubmit"
      >
        <a-form-item
            name="userAccount"
            :rules="[{ required: true, message: '请输入账号!' }]"
        >
          <a-input
              v-model:value="formState.userAccount"
              placeholder="请输入账号"
              class="custom-input"
          >
            <template #prefix>
              <UserOutlined class="input-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
            name="userPassword"
            :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input-password
              v-model:value="formState.userPassword"
              placeholder="请输入密码"
              class="custom-input"
          >
            <template #prefix>
              <LockOutlined class="input-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a-button
              type="primary"
              html-type="submit"
              class="login-btn"
              :loading="loginUserStore.loading"
          >
            登录系统
          </a-button>
        </a-form-item>

        <div class="action-links">
          <router-link to="/user/register" class="link">立即注册</router-link>
          <span class="divider">|</span>
          <router-link to="/forgot-password" class="link">忘记密码</router-link>
        </div>
      </a-form>
    </div>
  </div>
</template>

<style scoped lang="less">
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

@keyframes gradient {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
  position: relative;
  overflow: hidden;
}

.animated-background {
  .shape {
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    animation: float 6s infinite;

    &.circle {
      width: 200px;
      height: 200px;
      border-radius: 50%;
      top: 20%;
      left: 10%;
    }

    &.triangle {
      width: 0;
      height: 0;
      border-left: 100px solid transparent;
      border-right: 100px solid transparent;
      border-bottom: 180px solid rgba(255, 255, 255, 0.1);
      top: 50%;
      right: 15%;
    }

    &.square {
      width: 150px;
      height: 150px;
      bottom: 10%;
      left: 20%;
    }
  }
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 450px;
  z-index: 1;
  transform: translateY(0);
  transition: transform 0.3s, box-shadow 0.3s;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.3);
  }
}

.header {
  text-align: center;
  margin-bottom: 40px;

  .title {
    font-size: 26px;
    color: #2d3748;
    margin-bottom: 10px;
    font-weight: 600;
  }

  .subtitle {
    color: #718096;
    font-size: 14px;
  }
}

.login-form {
  .custom-input {
    height: 45px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    transition: all 0.3s;

    &:hover {
      border-color: #667eea;
    }

    &:focus {
      box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
    }

    :deep(.ant-input-prefix) {
      margin-right: 10px;
      color: #a0aec0;
    }
  }
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
  transition: all 0.3s;

  &:hover {
    transform: scale(1.02);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
  }

  &:active {
    transform: scale(0.98);
  }
}

.action-links {
  text-align: center;
  margin-top: 20px;

  .link {
    color: #718096;
    transition: color 0.3s;

    &:hover {
      color: #667eea;
    }
  }

  .divider {
    color: #cbd5e0;
    margin: 0 12px;
  }
}

@media (max-width: 768px) {
  .login-card {
    margin: 20px;
    padding: 30px;

    .title {
      font-size: 22px;
    }
  }

  .animated-background {
    display: none;
  }
}
</style>
